package com.debao;

import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.Constants;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.Coach;
import com.debao.system.domain.CourseCard;
import com.debao.system.domain.Evaluate;
import com.debao.system.domain.Interest;
import com.debao.system.domain.vo.AppointReqVo;
import com.debao.system.domain.vo.CoachReqVo;
import com.debao.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 教练
 */
@RestController
@RequestMapping("/api/coach")
public class ApiCoach {


    @Resource
    private ICoachService coachService;
    @Resource
    private ICourseCardService courseCardService;
    @Resource
    private IInterestService interestService;

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private IConfigService configService;


    /**
     * 教练列表
     *
     * @param vo
     * @return
     */
    @GetMapping("/list")
    @Anonymous
    public AjaxResult coachList(CoachReqVo vo) {
        return AjaxResult.success(coachService.selectCoachList(new Coach().setStadiumId(vo.getStadiumId()).setStatus(Constants.STATUS_1).setIsDel(Constants.IS_DEL_N).setKeyWord(vo.getKeyWord()).setColumn(vo.getOrderBy()).setSort(vo.getSort())));
    }

    /**
     * 教练详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @Anonymous
    public AjaxResult coachList(@PathVariable Long id) {
        return AjaxResult.success(
                new HashMap<String, Object>() {{
                    Long loginUserId = SecurityUtils.getLoginUserId();
                    Coach coach = coachService.selectCoachById(id);
                    Boolean isInterested = false;
                    if (loginUserId != null){
                        isInterested= interestService.selectInterestByUserId(loginUserId,coach.getUserId());
                    }
                    String isPlay = "";
                    isPlay = configService.selectValueByKey(Constants.IS_PLAY);
                    // 教练信息
                    put("info", coach);
                    put("isFollow",isInterested);
                    put("isPlay",isPlay);
                    // 课程卡信息
                    put("classCard", courseCardService.selectCourseCardList(new CourseCard().setCoachId(id).setOnline(1L)));
                }});
    }
    /**
     * 教练评价信息
     */
    @GetMapping("/evaluate/{id}")
    @Anonymous
    public AjaxResult evaluateList(@PathVariable Long id){
        //课程评论信息
        List<Evaluate> evaluates = evaluateService.selectEvaluateList(new Evaluate().setUnionId(id));
        return AjaxResult.success(evaluates);
    }

    /**
     * 教练入驻
     */
    @PostMapping("/enterCoach")
    public AjaxResult enterCoach(@RequestBody @Validated Coach coach) {
        coach.setUserId(SecurityUtils.getUserId());
        return coachService.insertCoach(coach) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 教练详情
     */
    @GetMapping("/coachInfo")
    public AjaxResult coachInfo(){
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(coachService.selectCoachByUserId(userId));
    }

    /**
     * 修改教练
     */
    @PostMapping("/updateCoach")
    public AjaxResult updateCoach(@RequestBody @Validated Coach coach) {
        return coachService.updateCoach(coach) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 关注教练
     */
    @PostMapping("/interest/{userId}")
    public AjaxResult interest(@PathVariable("userId") Long userId) {
        int row = interestService.insertInterest(new Interest().setOtherId(userId).setUserId(SecurityUtils.getUserId()));
        if (row == 1) {
            coachService.updateUploadFans(userId, "add");
            return AjaxResult.success("关注成功");
        }
        return AjaxResult.error("关注失败");
    }

    /**
     * 取消关注教练
     */
    @DeleteMapping("delInterest/{userId}")
    public AjaxResult delInterest(@PathVariable("userId") Long userId) {
        int row = interestService.deleteInterest(userId, SecurityUtils.getUserId());
        if (row == 1) {
            coachService.updateUploadFans(userId, "reduce");
            return AjaxResult.success("取消关注成功");
        }
        return AjaxResult.error("取消关注失败");
    }

    /**
     * 教练评价
     */
    @PostMapping("/addEvaluate")
    public AjaxResult addEvaluate(@RequestBody Evaluate evaluate) {
        evaluate.setUserId(SecurityUtils.getUserId());
        int row = evaluateService.insertEvaluate(evaluate);
        if(row==1){
            return AjaxResult.success("评价成功");
        }
        return AjaxResult.error("评价失败");
    }
}
